<?php
/**
 * desc Created by wang.
 * User: wang
 * motto: 是时候为小时候吹过的牛而努力了!
 * Date: 2020/3/23 16:55
 */

namespace app\common\model\mysql;


use think\Exception;
use think\facade\Session;
use think\Model;

class Log extends Model
{
    protected $name = 'admin_log';
    protected $autoWriteTimestamp = true;

    /**
     * 功能说明 写入管理员操作日志记录
     * @author: wang
     * @Date: 2020/4/3 11:29
     * @param $content
     * @return bool
     * @throws Exception
     */
    public function insert_admin_log($content)
    {
        if (session('?session_admin')) {
            $req = request()->param();
            if (!empty($req['password']) || !empty($req['password1']) || !empty($req['password2'])) {
                $param = '*******';
            } else {
                $param = json_encode(request()->param(), JSON_UNESCAPED_UNICODE);
            }
            $data = [
                'admin_id' => Session::get('session_admin.id'),
                'username' => Session::get('session_admin.username'),
                'useragent' => request()->server('HTTP_USER_AGENT'),
                'ip' => request()->ip(),
                'url' => request()->url(true),
                'method' => request()->method(),
                'type' => request()->type(),
                'param' => $param,
                'content' => $content,
            ];
            try {
                $this->save($data);
            } catch (\Exception $e) {
                throw new \think\Exception('数据库内部异常');
            }

            return true;
        }

    }


    /**
     * 功能说明 获取日志记录列表
     * @author: wang
     * @Date: 2020/4/3 11:31
     * @param $where
     * @param int $limit
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getLogList($where, $limit = 15)
    {
        return $this->where($where)->order('id desc')->paginate($limit);
    }
}